
# Generic Abstract Rule
class Rule ():
#     Boolean result;
#     String goodMessage;
#     String badMessage;

     def brokeRule (self, timeblocks): pass

     def getMessage (self):
         if (self.result):
             return self.badMessage
         return self.goodMessage

     def __init__(self, timeblocks):
         self.result = self.brokeRule(timeblocks)



# if drinking at all
class DrinkingRule (Rule):
    drinkingTips = \
        "	- Avoid drinking games<br />"+\
        "	- Alternate non-alcoholic with alcoholic drinks<br />"+\
        "	- Choose not to drink alcohol<br />"+\
        "	- Eat before and/or during drinking<br />"+\
        "	- Determine in advance not to exceed a set number of drinks<br />"+\
        "	- Stick with only one kind of alcohol when drinking<br />"+\
        "	- Know your limit <br />"+\
        "	- Use a designated driver<br />"+\
        "	- Stay with the same group of friends the entire time drinking<br />"+\
        "	- Pace drinks to one or fewer an hour <br />"+\
        "	- Keep track of how many drinks you have consumed <br />"+\
        "	- Have a friend let you know when you have had enough to drink</p>"
    goodMessage = \
        "<h3 class='good'>Good job for not drinking!</h3>"+\
        "It's good that you choose not to drink alcohol. If you do choose to drink, however, be sure to use the protective strategies bulleted below. Alcohol misuse is highly correlated with lower grades, increased stress, and impaired sleep. If someone you know needs help with alcohol misuse, be sure to use the resources available at <a href='http://well.wvu.edu/alcohol_drug_ed'>http://well.wvu.edu/alcohol_drug_ed</a>, or contact WELLWVU Student Assistance Program at 304-293-WELL (9355).<br />"+drinkingTips
    badMessage = \
        "<h3 class='risk'>Health risk: drinking</h3>"+\
        "If you choose to drink alcohol, be sure to use the protective strategies bulleted below. Think about the frequency and quantity of alcohol you're consuming and honestly consider its impact on your quality of life. Alcohol misuse is highly correlated with lower grades, increased stress, and impaired sleep. If you or someone you know needs help with alcohol misuse, be sure to use the resources available at <a href='http://well.wvu.edu/alcohol_drug_ed'>http://well.wvu.edu/alcohol_drug_ed</a>, or contact WELLWVU Student Assistance Program at 304-293-WELL (9355).<br />"+drinkingTips
    
    def brokeRule (self, timeblocks):
        for tb in timeblocks:
            if (tb['activity'] == "drinking"):
                return True
        return False


# eating breakfast every day
class EatingRule1 (Rule):
    goodMessage = \
        "<h3 class='good'>Good job eating breakfast!</h3>"+\
        "It's the most important meal of the day. It helps regulate your metabolism and recharges your brain and body after your slumber. Eating breakfast and eating regular meals, helps you maintain optimal energy levels throughout the day and a healthy body weight. For more information on how to eatWELL visit our website at <a href='http://well.wvu.edu/nutrition'>http://well.wvu.edu/nutrition</a>."
    badMessage = \
        "<h3 class='risk'>Health risk: no breakfast</h3>"+\
        "Make sure you eat breakfast every morning. Breakfast is the most important meal of the day. It helps regulate your metabolism and recharges your brain and body after your slumber.  Eating breakfast and eating regular meals, helps you maintain optimal energy levels throughout the day and a healthy body weight. For more information on how to eatWELL visit our website at <a href='http://well.wvu.edu/nutrition'>http://well.wvu.edu/nutrition</a>."

    # check to see if they ate in the AM every day
    def brokeRule (self, timeblocks):
        days = [True, True, True, True, True, True, True]
        
        for tb in timeblocks:
            if (tb['activity'] == "eating"):
                if (tb['hour'] < 12):
                    days[tb['day']] = False;

        return (days[0] or days[1] or days[2] or days[3] or days[4] or days[5] or days[6])


# eating 3 times a day
class EatingRule2 (Rule):
    goodMessage = \
        "<h3 class='good'>Good job for eating 3 times a day!</h3>"+\
        "You have a very healthy eating schedule. Remember to keep eating 3 times a day. Snacking between meals is fine, as long as you consider your snacks a part of your overall healthy intake. Make fruits, vegetables, and whole grains a primary focus of your daily intake. Shoot for at least 5 servings of fruits and vegetables each day and make at least half of your grains whole grains. For more information on how to eatWELL visit our website at <a href='http://well.wvu.edu/nutrition'>http://well.wvu.edu/nutrition</a>."
    badMessage = \
        "<h3 class='risk'>Health risk: not eating 3 times a day</h3>"+\
        "Reconsider your eating habits. Shoot for eating at least 3 times a day. Snacking between meals is fine, as long as you consider your snacks a part of your overall healthy intake. Make fruits, vegetables, and whole grains a primary focus of your daily intake. Shoot for at least 5 servings of fruits and vegetables each day and make at least half of your grains whole grains. For more information on how to eatWELL visit our website at <a href='http://well.wvu.edu/nutrition'>http://well.wvu.edu/nutrition</a>."

    # check if user ate less than 3 times in a day for all 7 days
    def brokeRule (self, timeblocks):
        for i in range(7):
            # pull out all the eating blocks from a single day
            eatingBlocks = filter(lambda tb: tb['day'] == i and tb['activity'] == 'eating',
                                  timeblocks)
            if len(eatingBlocks) < 3 : return True
            
        return False


# Exercising 2.5 hours a week
class ExerciseRule (Rule):
    goodMessage = \
        "<h3 class='good'>Good work exercising!</h3>"+\
        "Exercise is a great stress reducer and energy booster, and can help you maintain a healthy body weight. People who are physically active reduce their risk of developing major chronic diseases, such as coronary heart disease, stroke and type II diabetes, by up to 50%. For ways to stay active or get fit visit the WELLWVU website at <a href='http://well.wvu.edu/fitness'>http://well.wvu.edu/fitness</a>."
    badMessage = \
        "<h3 class='risk'>Health Risk: not enough exercise</h3>"+\
        "It doesn't look like you exercise very often. Exercise recommendations for college students and young adults are to exercise at least 30 minutes most days of the week (at least 5). Exercise is a great stress reducer and energy booster, and can help you maintain a healthy body weight.  People who are physically active reduce their risk of developing major chronic diseases, such as coronary heart disease, stroke and type II diabetes, by up to 50%.  For ways to stay active or get fit visit the WELLWVU website at <a href='http://well.wvu.edu/fitness'>http://well.wvu.edu/fitness</a>."

    # check if exercised for 2.5 hours
    def brokeRule (self, timeblocks):
        totaltime = 0
        for tb in timeblocks:
            if tb['activity'] == 'exercising' : totaltime += tb['duration']
        return totaltime < 2.5
        

# Sleeping 7+ hours a day
class SleepRule1 (Rule):
    goodMessage = \
        "<h3 class ='good'>Good job sleeping!</h3>"+\
        "It looks like you're getting plenty of sleep. This improves your alertness, which affects the way your brain functions, your reaction time, and your mood. Remember to keep getting at least 7 to 8 hours of sleep a night. If needed, use ear plugs, eye masks, and fans to achieve even better quality sleep. For more tips on how to sleepWELL visit our website at <a href='http://well.wvu.edu/lifestyle_fitness'> http://well.wvu.edu/lifestyle_fitness</a>."
    badMessage = \
        "<h3 class='risk'>Health risk: not enough sleep</h3>"+\
        "Like many college students, you're not getting enough sleep. This can impair your alertness, which affects the way your brain functions, your reaction time, and your mood. Remember to keep getting at least 7 to 8 hours of sleep a night. If needed, use ear plugs, eye masks, and fans to achieve even better quality sleep. For more tips on how to sleepWELL visit our website at <a href='http://well.wvu.edu/lifestyle_fitness'> http://well.wvu.edu/lifestyle_fitness</a>."
    
    # check if user got an average of 7 hours (343 min) or more sleep a night
    def brokeRule (self, timeblocks):
        totaltime = 0
        for tb in timeblocks:
            if tb['activity'] == 'sleeping' : totaltime += tb['duration']
        return totaltime < 49


# No 2 days can have a difference of 1 hours sleep
class SleepRule2 (Rule):
    goodMessage = \
        "<h3 class ='good'>Excellent sleep schedule!</h3>"+\
        "Consistent sleep patterns maintain your body's biorhythms and make it easier for you to optimally function. Keep up this good habit and remember: Same Time. Same Place. 8.0. Try and go to bed at the same time, in the same place for the same amount of time (roughly 8 hours) every night.  Also make sure that your room is dark, cool, and quiet for the best quality sleep. For more tips on how to sleepWELL visit our website at <a href='http://well.wvu.edu/lifestyle_fitness'> http://well.wvu.edu/lifestyle_fitness</a>."
    badMessage = \
        "<h3 class='risk'>Health risk: poor sleep schedule</h3>"+\
        "Good quality, regular sleep is more important than you might think. Changing sleep patterns on a regular basis alters your body's biorhythms and makes it harder for you to optimally function. A good rule of thumb to keep in mind is: Same Time. Same Place. 8.0. Try and go to bed at the same time, in the same place for the same amount of time (roughly 8 hours) every night to develop good sleeping habits. Make sure that your room is dark, cool and quiet for the best quality sleep. For more tips on how to sleepWELL visit our website at <a href='http://well.wvu.edu/lifestyle_fitness'>http://well.wvu.edu/lifestyle_fitness</a>."

    def brokeRule (self, timeblocks):
         wakeHours = [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
         sleepHours = [24.0, 24.0, 24.0, 24.0, 24.0, 24.0, 24.0]
         for i in range(7):
              singleDay = sorted( filter( lambda tb : tb['day'] == i, timeblocks ), 
                                  key=lambda tb: tb['hour'])
              for tb in singleDay:
                   if tb['activity'] == 'sleeping': 
                        wakeHours[i] = tb['hour'] + tb['duration']
                        break
              for tb in reversed(singleDay):
                   if tb['activity'] == 'sleeping': 
                        sleepHours[i] = tb['hour']
                        break

         for h1 in wakeHours:
              for h2 in wakeHours:
                   if abs(h1-h2) >= 1: return True
         for h1 in sleepHours:
              for h2 in sleepHours:
                   if abs(h1-h2) >= 1: return True
              
         return False

            
# need to have more than 0 hours of socializing
class SocializeRule (Rule):
    goodMessage = \
        "<h3 class ='good'>Good job for being social!</h3>"+\
        "It's good that you find time to socialize. College is a great time to meet new people and discover new aspects of yourself. For more information on activities on campus visit <a href='http://apply.wvu.edu/visit/placesaroundcampus.aspx'>http://apply.wvu.edu/visit/placesaroundcampus.aspx</a>"
    badMessage = \
        "<h3 class='risk'>Health risk: no socializing</h3>"+\
        "College is a great time to meet new people and discover new aspects of yourself. Make sure to plan time for friends and chillWELL to get away from everyday stress. Take some time to roam around campus and town and see what it has to offer. Catch a show at the Creative Arts Center, watch a movie at the Mountainlair movie room or take advantage of the mountains and go on a trip with Adventure West Virginia. You just may be surprised at what you find and the fun you'll have doing it. For more information on activities on campus visit <a href='http://apply.wvu.edu/visit/placesaroundcampus.aspx'>http://apply.wvu.edu/visit/placesaroundcampus.aspx</a>"
    
    def brokeRule (self, timeblocks):
        for tb in timeblocks:
            if tb['activity'] == 'socializing' : return False
        return True


# need to have more than 0 hours
class SpiritualRule (Rule):
    goodMessage = \
        "<h3 class ='good'>Good job for connecting!</h3>"+\
        "It's good that you set aside time to connect. Spirituality is important for many people and is very individualized. For some it means time in nature for others it means being in a place of worship. For more information on religious organizations on campus visit: <a href = 'http://www.campuslight.org/wvu/'> http://www.campuslight.org/wvu/</a>"
    badMessage = \
        "<h3 class ='risk'>Health risk: no spiritual time</h3>"+\
        "Consider setting aside time to connect. Spirituality is important for many people and is very individualized. For some it means time in nature for others it means being in a place of worship. Think about what you need to renew. For more information on religious organizations on campus visit <a href = 'http://www.campuslight.org/wvu/'> http://www.campuslight.org/wvu/</a>"

    def brokeRule (self, timeblocks):
        for tb in timeblocks:
            if tb['activity'] == 'spiritual' : return False
        return True



# must study more than 18 hours per week
class StudyRule (Rule):
    goodMessage = \
        "<h3 class='good'>Good job studying!</h3>"+\
        "It looks like you study diligently. Setting aside time for your studies can reduce the need for cramming and increase your ability to concentrate and retain information. For more resources on studying and time management, check out our website: <a href='http://well.wvu.edu/stress_management'>http://well.wvu.edu/stress_management</a>."
    badMessage = \
        "<h3 class='risk'>Health risk: not enough studying</h3>"+\
        "It doesn't look like you study very much. For each one credit hour in a class it is recommended that you study at least 2 (ideally 3) hours per week outside of class. That means if you are registered for 3 3-credit classes you should devote at least 18 hours weekly to homework and studying. Setting aside this time for your studies can reduce the need for cramming and increase your ability to concentrate and retain information.  A good study plan will help improve your grade point average and decrease your stress level.  For more resources on studying and time management, check out our website: <a href='http://well.wvu.edu/stress_management'>http://well.wvu.edu/stress_management</a>."

    def brokeRule (self, timeblocks):
         studytime = 0.0;
         classtime = 0.0;
         for tb in timeblocks:
              if tb['activity'] == 'studying': studytime += tb['duration']
              if tb['activity'] == 'class': classtime += tb['duration']
         return studytime < 2*classtime


# need more than 0 hours
class WorkRule (Rule):
    goodMessage = \
        "<h3 class='good'>Good work!</h3>"+\
        "Good job finding work and community service. Your time in college is a great chance to give back to the community. If you are interested, there are a number of student oranizations, campus jobs, and community services offered through WVU. For more information on student organizations on campus visit <a href='http://sos.wvu.edu/organization_listing'>http://sos.wvu.edu/organization_listing</a>. For information on volunteering visit <a href='http://iserve.wvu.edu/custom/1330'>http://iserve.wvu.edu/custom/1330/</a>. For a listing of job opening on campus visit <a href='http://studentemployment.hr.wvu.edu'>http://studentemployment.hr.wvu.edu/</a>."
    badMessage = \
        "<h3 class='risk'>Health risk: no work or community service</h3>"+\
        "The college experience is more than just your time to learn and grow but it's your chance to give back. WVU has many opportunities for you to volunteer for community service or get involved in a student organization that is aimed at giving back to the community or helping students on campus. There are also a number of on campus jobs that may lead to career building opportunities. For more information on student organizations on campus visit <a href='http://iserve.wvu.edu/custom/1330'>http://iserve.wvu.edu/custom/1330/</a>. For a listing of job opening on campus visit <a href='http://studentemployment.hr.wvu.edu'>http://studentemployment.hr.wvu.edu/</a>."

    def brokeRule (self, timeblocks):
        for tb in timeblocks:
            if tb['activity'] == 'work' : return False
        return True
